﻿/**
 * ...
 * @author AreS
 */

package gui.hug;

import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextFormat;

class HUGList extends HUGSprite
{
	private static inline var UP_BUTTON_NAME : String = "upButton";
	private static inline var DOWN_BUTTON_NAME : String = "downButton";
	
	private var elements : Array < HUGSprite >;
	private var elementsCount : Int;
	private var visibleElementsCount : Int;
	private var elementHeight : UInt;
	private var firstVisibleElement : Int;
	
	private var upButton : HUGButton;
	private var downButton : HUGButton;
	private var label : HUGLabel;
	
	private var showedElements : Array < HUGSprite>;
	
	public function new(elementHeight : Int, visibleElementsCount : Int, elements : Array < HUGSprite >, elementsCount : Int) {
		super();
		this.elementHeight = elementHeight;
		this.visibleElementsCount = visibleElementsCount;
		this.elementsCount = elementsCount;
		this.firstVisibleElement = 0;
		this.elements = elements;
		this.showedElements = new Array < HUGSprite >();
		drawList();
	}
	
	private function drawList() {
		if (getPagesCount() != 1) {
			upButton = new HUGLinkButton("<", new TextFormat("Arial", 18, HUGColors.VK_BUTTON), HUGColors.WHITE);
			upButton.addEventListener(MouseEvent.CLICK, onUpButtonClick);
			upButton.setLocation(elements[1].width / 2 - 50 - upButton.width, elementHeight * visibleElementsCount + 10);
			upButton.name = UP_BUTTON_NAME;
			this.addChild(upButton);
			
			label = new HUGLabel();
			label.text = "1 из " + getPagesCount();
			label.setTextFormat(new TextFormat("Arial", 13, HUGColors.VK_PROPERTY_NAME_TEXT));
			label.setLocation(elements[1].width / 2 - label.width / 2, elementHeight * visibleElementsCount + 12);
			this.addChild(label);
			
			downButton = new HUGLinkButton(">", new TextFormat("Arial", 18, HUGColors.VK_BUTTON), HUGColors.WHITE);
			downButton.addEventListener(MouseEvent.CLICK, onDownButtonClick);
			downButton.setLocation(elements[1].width / 2 + 50, elementHeight * visibleElementsCount + 10);
			downButton.name = DOWN_BUTTON_NAME;
			this.addChild(downButton);
		}

		showNewElements();
	}
	
	private function showNewElements() {
		var lastVisibleElement = getLastVisibleElement();
		var i : Int = firstVisibleElement;
		while (i < lastVisibleElement) {
			elements[i].setLocation(0, getElementY(i - firstVisibleElement));
			elements[i].name = Std.string(i);
			this.addChild(elements[i]);
			showedElements.push(elements[i]);
			i++;
		}
	}
		
	private function getLastVisibleElement() : Int {
		var i : Int = firstVisibleElement + visibleElementsCount;
		if (elementsCount < i) {
			return elementsCount;
		}
		return i;
	}
	
	private function getElementY(index : Int) : Float {
		return index * elementHeight;
	}
	
	private function onUpButtonClick(e : MouseEvent) {
		if (firstVisibleElement > 0) {
			firstVisibleElement -= visibleElementsCount;
			label.text = (firstVisibleElement / visibleElementsCount + 1) + " из " + getPagesCount();	
			label.setTextFormat(new TextFormat("Arial", 13, HUGColors.VK_PROPERTY_NAME_TEXT));
			removeCurrentElements();
			showNewElements();
		}
	}
	
	private function onDownButtonClick(e : MouseEvent) {
		if (firstVisibleElement + visibleElementsCount < elements.length) {
			firstVisibleElement += visibleElementsCount;
			label.text = (firstVisibleElement / visibleElementsCount + 1) + " из " + getPagesCount();
			label.setTextFormat(new TextFormat("Arial", 13, HUGColors.VK_PROPERTY_NAME_TEXT));
			removeCurrentElements();
			showNewElements();
		}		
	}
	
	private function getPagesCount() : Int {
		if (elementsCount == visibleElementsCount) return 1;
		var add : Int = 1;
		if (elementsCount % visibleElementsCount == 0) add = 0;
		return Math.floor(elementsCount / visibleElementsCount) + add;
	}
	
	private function removeCurrentElements() {
		for (e in showedElements) {
			this.removeChild(e);
		}
		showedElements = new Array < HUGSprite >();
	}
	
	public function addElements(elems : Array < HUGSprite > ) {
		this.elements = this.elements.concat(elems);
	}
}